// pages/my/mycollections.js
const app =  getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.onGetColletionTopics();
  },

  onShow: function(){
    this.onGetColletionTopics();
  },

  //得到收藏帖子列表
  onGetColletionTopics: function(){
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    wx.request({
      url: app.globalData.targetUrl + 'discuss/topics/collect',
      data:{
        user_id: user_id
      },
      success: res => {
        console.log(res.data);
        that.setData({
          items: res.data.data
        })
      }
    })
  },

  //取消帖子收藏
  onCancelCollection: function(e){
    var that = this;
    var user_id = wx.getStorageSync('user_id');
    var topic_id = e.currentTarget.dataset.topic_id;
    wx.request({
      url: app.globalData.targetUrl + 'discuss/topics/collect',
      //method: 'DELETE',
      method: 'GET',
      data:{
        user_id: user_id,
        topic_id: topic_id,
        is_delete: 1
      },
      success: res => {
        wx.showToast({
          title: '取消收藏成功'
        })
        console.log(res.data);
        that.onGetColletionTopics();
      }
    })
  },

  //点击跳转至该帖子的详情页面
  onToTopic: function(e){
    var topic_id = e.currentTarget.dataset.topic_id;
    wx.navigateTo({
      url: '/pages/lifeCircle/comments?topic_id=' + topic_id,
    })
  }
})